package io.netty.protocodec;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import lombok.extern.slf4j.Slf4j;

/**
 * 自定义协议编码器
 *
 * @author Verlet
 * @date 2023-02-11 00:43
 */
@Slf4j
public class MsgEncoder extends MessageToByteEncoder<MsgProto> {
    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, MsgProto msgProto, ByteBuf byteBuf) throws Exception {
        log.info("==========调用编码器==========");
        int length = msgProto.getLength();
        byte[] data = msgProto.getData();
        byteBuf.writeInt(length);
        byteBuf.writeBytes(data);
    }
}
